﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPS.Models;
using MyPSDALayer;
using System.Data;

namespace MyPS.Areas.Manage.Controllers
{
    [Authorize]
    public class ManageResourceTypeController : Controller
    {
        private MyPSEntities db = new MyPSEntities();
        //
        // GET: /Manage/ManageResourceType/

        public ActionResult Index()
        {
            return View(db.ResourceTypes.ToList());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ResourceType type)
        {
            if (ModelState.IsValid)
            {
                if (db.ResourceTypes.Where(m => m.ResourceTypeName == type.ResourceTypeName).Count() > 0)
                {
                    ModelState.AddModelError("", "资源名称重复");
                }
                else
                {
                    db.ResourceTypes.Add(type);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return View(type);
        }

        public ActionResult Edit(string ResourceTypeName)
        {
            var mode = db.ResourceTypes.Find(ResourceTypeName);
            if (mode == null)
            {
                return HttpNotFound();
            }
            return View(mode);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ResourceType mode)
        {
            if (ModelState.IsValid)
            {
                db.Entry<ResourceType>(mode).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(mode);
        }

        public ActionResult Delete(string ResourceTypeName = "")
        {
            var mode = db.ResourceTypes.Find(ResourceTypeName);
            if (mode == null)
            {
                return HttpNotFound();
            }
            return View(mode);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string ResourceTypeName = "")
        {
            ResourceType type = db.ResourceTypes.Find(ResourceTypeName);
            db.ResourceTypes.Remove(type);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
